package com.dtw.ditouwang.ui.more;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.MoreBean;
import com.dtw.ditouwang.ui.BaseFragment;
import com.dtw.ditouwang.ui.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MoreNewsFragmment extends BaseFragment implements OnItemClickListener {
    public static final String KeyID = "KeyID";
    public static final String KeyType = "keyType";
    public static final String KeyTitle = "KeyTitle";
    EditorChooseNormalAdapter editorChooseNormalAdapter;
    List<MoreBean.DataBean> dataBeanList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_editor_choose_normal_content, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.titleview).setVisibility(View.GONE);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
        editorChooseNormalAdapter = new EditorChooseNormalAdapter(dataBeanList);
        editorChooseNormalAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(editorChooseNormalAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        webApi.getMore(3).enqueue(new Callback<MoreBean>() {
            @Override
            public void onResponse(Call<MoreBean> call, Response<MoreBean> response) {
                if (response.body().isResult()&&getActivity()!=null&&!getActivity().isFinishing()) {
                    dataBeanList.clear();
                    dataBeanList.addAll(response.body().getData());
                    editorChooseNormalAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<MoreBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onItemClick(int resID, int position, int state) {
        Intent intent = new Intent(getContext(), EditorChooseNormalDetailActivity.class);
        intent.putExtra(EditorChooseNormalDetailActivity.KeySYSID, dataBeanList.get(position).getId());
        startActivity(intent);
    }
}
